import java.io.*;
import java.util.Scanner;

public class BufferedReaderAndReaderFile {
    public static void main(String[] args)  {
        Scanner input = new Scanner(System.in);
        String word = "1998\\法国\n2002\\巴西\n2006\\意大利\n2010\\西班牙\n2014\\德国\n2018\\法国";
        File file1 = new File("E:\\myFile","wordcup.txt");

        System.out.println( file1.exists());
        //在指定目录中创建文件
        try {
            file1.createNewFile();
        }catch (IOException e){
            e.printStackTrace();
        }
        FileOutputStream fs = null;
        try{
            //创建文件输出流对象
            fs = new FileOutputStream("E:\\myFile\\wordcup.txt",false);
            //将内存中的数据写入
            byte[] date = word.getBytes("UTF-8");
            fs.write(date);
            fs.flush();//清空缓冲区的临时数据
            System.out.println("写入成功！");


        }catch (IOException e){
            e.printStackTrace();

        }
        finally {
            try{
                if(fs != null ){
                    fs.close();
                    fs =null;
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        try {
            //创建一个FileReader对象
            FileReader fr = new FileReader("E:\\myFile\\wordcup.txt");
            //创建一个BufferedReade对象
            BufferedReader bufferedReader = new BufferedReader(fr);
            System.out.print("请输入举办世界杯的年份");
            String year = input.next();

            //读取一行数据
            String line = bufferedReader.readLine();
            System.out.println(line);
            while (line!=null){
                if(line.substring(0,4).equals(year)){
                    System.out.println(line);
                    return;
                }
                line = bufferedReader.readLine();
                System.out.println(line);
            }
            System.out.println("没有查询到！");
        }catch (IOException e){
            e.printStackTrace();
        }


    }

}
